<?php
if (!isset($_SESSION['login']) || !$_SESSION['login']==true)
{
	include 'login.php';
}
?>
<script type="text/javascript" src="lib/js/jquery.js"></script>
<script src="lib/js/elrte/js/jquery-ui-1.8.7.custom.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="lib/js/elrte/css/smoothness/jquery-ui-1.8.7.custom.css" type="text/css" media="screen" charset="utf-8">

<script src="lib/js/elrte/js/elrte.min.js" type="text/javascript" charset="utf-8"></script>
	<link rel="stylesheet" href="lib/js/elrte/css/elrte.min.css" type="text/css" media="screen" charset="utf-8">



<h1>admin</h1>

<div>
	<a href="index.php?action=admin&sub_action=inserisci">Inserisci nuova pagina</a>
	<a href="index.php?action=admin&sub_action=admin">Elenco</a>
    <a href="index.php?action=logout">Log-Out</a>
</div>
<?php
require_once'funzioni.php';
connettiDb();

switch ($_REQUEST['sub_action']){
			case 'elimina':eliminaPagina();break;	
			case 'modificaDb':modificaDb();break;
			case 'modifica':modificaPagina();break;
			case 'inserisci':visualizzaForm();break;
			case 'inserisciDb':inserisciPaginaDb();break;
			
			default:adminListPages();break;
			}

function eliminaPagina()
{
	$sql="DELETE FROM pages WHERE id=$_GET[id]";
	//echo $sql;
	if(mysql_query($sql))
	{
		echo '<h2 class="msg">Dati Eliminato</h2>';
		adminListPages();
	}
	else
	{
		echo '<h2 class="msg">Errore</h2>';
	}
	
}

function modificaDb()
{
	$content_it= mysql_real_escape_string($_POST[content_it]);	
	$title_it= mysql_real_escape_string($_POST[title_it]);
	$sql=sprintf("UPDATE pages SET title_it='%s', title_en='%s', content_it='%s', content_en='%s', section='%s' WHERE id=%s",
	$title_it,
	$_POST['title_en'],
	$content_it,
	$_POST['content_en'],
	$_POST['section'],
	$_POST['id']
	
	);
	//echo $sql;
	if(mysql_query($sql))
			echo '<h2 class="msg">Dati Modificati</h2>';
		else
			echo '<h2 class="msg">Errore</h2>';
}

function modificaPagina()
{
	echo $_GET['id'];	
	$sql="SELECT * FROM pages WHERE id=$_GET[id]";
	$result=mysql_query($sql);
	if (mysql_num_rows($result));
	{
		$dati=mysql_fetch_assoc($result);
		visualizzaForm($dati, 'modificaDb');
	}
}

function inserisciPaginaDb()
{  
	$content_it= mysql_real_escape_string($_POST[content_it]);	
	$title_it= mysql_real_escape_string($_POST[title_it]);
	$sql="INSERT into pages (title_it, title_en, content_it,content_en,section)
			VALUES('$title_it','$_POST[title_en]',
			'$content_it','$_POST[content_en]',
			'$_POST[section]')";
			mysql_query($sql);
			echo '<h2 class="msg">Dati Inseriti</h2>';

}

function VisualizzaForm($dati=array(), $sub_action='inserisciDb')
{
		include 'config.php'; ?>
		<form action="index.php?action=admin" method="post">
		<input type="hidden" name="sub_action" value="<?php echo $sub_action; ?>" />
		<input type="hidden" name="id" value="<?php echo $dati['id']; ?>" />
		<label>Titolo (it)</label>
		<input type="text" name="title_it" value="<?php echo $dati['title_it'];?>" /> <br /> 		
		<label>Titolo (en)</label>
		<input type="text" name="title_en" value="<?php echo $dati['title_en'];?>" /> <br />
		<label>Contenuto (it)</label>
		<textarea id="content_it" rows="5" cols="50" name="content_it"><?php echo $dati['content_it'];?></textarea> <br />
		<label>Contenuto (en)</label>
		<textarea id="content_en"rows="5" cols="50" name="content_en"><?php echo $dati['content_en'];?></textarea> <br />
		<label>Sezione</label>
		<select name="section">
		<?php foreach ($sections as $sezione=>$sottosezioni)
				{
				printf('<option value="%s" %s>%s</option>'	
					,$sezione, verificaStato($sezione, $dati['section']), $sezione);
				}
		?>

		</select>
		<input type="submit" value="invia" />
        </form>
        
        <script type="text/javascript">
        var opts = {
				cssClass : 'el-rte',
				// lang     : 'ru',
				height   : 150,
				toolbar  : 'complete',
				cssfiles : ['css/elrte-inner.css']
			}
			$('#content_it').elrte(opts);
        	$('#content_en').elrte(opts);
        </script>
		
		<?php
}

function verificaStato($valore1, $valore2)
{
	if ($valore1==$valore2)
			return'selected="selected"';
		else
			return '';
}


function AdminListPages()
{		
		$sql="select *
			from pages
			Order by title_it ASC";
			
		$result=mysql_query($sql);
		
		if(mysql_num_rows($result))
		{
		
			echo '<table>
					<tr>
						<th>id</th>
						<th>titolo</th>
						<th>Sezione</th>
						
						<th>Modifica</th>
						<th>Elimina</th>
						</tr>';
		
			while($row=mysql_fetch_assoc($result))
			{
			
			printf('<tr>
					<th>%s</th>
					<th>%s</th>
					<th>%s</th>
					
					<th>%s</th>
					<th>%s</th>
					</tr>', $row['id'],
							$row['title_it'], 
							$row['section'], 
							//substr($row['content_it'],0,50),
							'<a href="index.php?action=admin&sub_action=modifica&id='.$row['id'].'">Modifica</a>',
							'<a class="delete" href="index.php?action=admin&sub_action=elimina&id='.$row['id'].'">Elimina</a>');
			}
		echo '</table>';
		?>		
        <script type="text/javascript">
        	$('.delete').click(function(e){
				//alert(122);
				if(confirm('Sei sicuro di voler eliminare?'))
				{
					
				}
				else
					e.preventDefault();
			
			});
		</script>
		<?php
		}
}

?>